﻿using GroupThreeMESProject.DTO;
using GroupThreeMESProject.Entity;
using GroupThreeMESProject.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace GroupThreeMESProject.SchedulingTasksService
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SchedulingTasksDataService : ApplicationService
    {
        private readonly ISqlSugarClient _db;

        public SchedulingTasksDataService(ISqlSugarClient db)
        {
            _db = db;
        }
        /// <summary>
        /// 获得调度类型
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetSchedulingType")]
        public async Task<ApiResult<List<SchedulingType>>> GetSchedulingType()
        {
            var list = await _db.Queryable<SchedulingType>().ToListAsync();
            return ApiResult<List<SchedulingType>>.Success(list);
        }

        /// <summary>
        /// 获取中间表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetSchedulingTasksInventoryLocation")]
        public async Task<ApiResult<List<SchedulingTasksInventoryLocation>>> GetSchedulingTasksInventoryLocation()
        {
            var list = await _db.Queryable<SchedulingTasksInventoryLocation>().ToListAsync();
            return ApiResult <List<SchedulingTasksInventoryLocation>>.Success(list);
        }

        /// <summary>
        /// 添加调度任务
        /// </summary>
        /// <param name="schedulingtasks">调度任务实体</param>
        /// <returns></returns>
        [HttpPost("AddSchedulingTasks")]
        public async Task<ApiResult<string>> AddSchedulingTasks(SchedulingTasks schedulingtasks)
        {
            var result = await _db.Insertable(schedulingtasks).ExecuteCommandAsync();
            if (result == 0) return ApiResult<string>.Error("添加失败");
            return ApiResult<string>.Success("添加成功");
        }

        /// <summary>
        /// 调度任务显示分页
        /// </summary>
        /// <param name="schedulingtasksinput">输入参数</param>
        /// <returns></returns>
        [HttpPost("GetSchedulingTasksShow")]
        public async Task<ApiResult<FenYeDTO<SchedulingTasksDTO>>> GetSchedulingTasksShow(PageInPut schedulingtasksinput)
        {
            FenYeDTO<SchedulingTasksDTO> fenyeDTO = new FenYeDTO<SchedulingTasksDTO>();
            var list = await _db.Queryable<SchedulingTasks>().Where(x => x.task_status != 2 && x.task_status != 3)
                .LeftJoin<SchedulingTasksInventoryLocation>((a,b)=>a.LocationNo==b.LocationNo)
                .LeftJoin<SchedulingType>((a,b,c)=>a.scheduling_type_id==c.scheduling_type_id)
                .Select((a,b,c)=>new SchedulingTasksDTO
                {
                    scheduling_tasks_id = a.scheduling_tasks_id,
                    dispatch_number = a.dispatch_number,
                    scheduling_type_id = c.scheduling_type_id,
                    scheduling_type_name = c.scheduling_type_name,
                    scheduling_information = a.scheduling_information,
                    LocationNo = a.LocationNo,
                    id = b.id,
                    flow_box_number = b.flow_box_number,
                    MaterialNumbering = b.MaterialNumbering,
                    MaterialName = b.MaterialName,
                    moves_number = a.moves_number,
                    priority = a.priority,
                    task_status = a.task_status==0? "待执行" : "执行中",
                    time_out = a.time_out==false?"否":"是",
                    createtime = a.createtime,
                    requiredtime = a.requiredtime,
                    starttime = a.starttime,
                    completiontime = a.completiontime
                }).ToListAsync();
            fenyeDTO.totalCount = list.Count();
            fenyeDTO.pageCount = (int)Math.Ceiling(fenyeDTO.totalCount * 1.0 / schedulingtasksinput.pageSize);
            fenyeDTO.data = list.OrderByDescending(x=>x.scheduling_tasks_id).Skip((schedulingtasksinput.pageIndex-1)* schedulingtasksinput.pageSize).Take(schedulingtasksinput.pageSize).ToList();
            return ApiResult<FenYeDTO<SchedulingTasksDTO>>.Success(fenyeDTO);
        }

        //public async Task<ApiResult<>> GetSchedulingTasksInfo(int scheduling_tasks_id)
        //{
        //    var list = _db.Queryable<SchedulingTasks>().LeftJoin<Mes_InventoryLocation>((o,i)=>o.LocationNo==i.LocationNo)
        //}

        /// <summary>
        /// 修改状态--作业
        /// </summary>
        /// <param name="scheduling_tasks_id">调度任务id</param>
        /// <returns></returns>
        [HttpPost("UpdateStatusYes")]
        public async Task<ApiResult<string>> UpdateStatusYes(int scheduling_tasks_id)
        {
            var Tasks = await _db.Updateable<SchedulingTasks>().SetColumns(x => new SchedulingTasks()
            {
                task_status = 1,
                starttime = DateTime.Now
            }).Where(x=>x.scheduling_tasks_id== scheduling_tasks_id).ExecuteCommandAsync();
            if (Tasks > 0) return ApiResult<string>.Success("修改成功");
            return ApiResult<string>.Error("修改失败");
        }

        /// <summary>
        /// 修改状态--关闭
        /// </summary>
        /// <param name="scheduling_tasks_id">调度任务id</param>
        /// <returns></returns>
        [HttpPost("UpdateStatusNo")]
        public async Task<ApiResult<string>> UpdateStatusNo(int scheduling_tasks_id)
        {
            var Tasks = await _db.Updateable<SchedulingTasks>().SetColumns(x => new SchedulingTasks()
            {
                task_status =3,
                completiontime = DateTime.Now
            }).Where(x => x.scheduling_tasks_id == scheduling_tasks_id).ExecuteCommandAsync();
            if (Tasks > 0) return ApiResult<string>.Success("修改成功");
            return ApiResult<string>.Error("修改失败");
        }
    }
}
